$(function(){
	var isLogin = false;

	function render(){
		// 时事热点渲染
		renderData.sideNewList.forEach(function(elem){
			$(".s-news-rank-content").append(
				'<li class="news-meta-item" data-index="0">'+
					'<em class="news-icon"></em>'+
					'<span class="news-title">'+
						'<span class="title-link">'+
							'<a class="title-content" href="'+ elem.link +'" target="_blank">'+ elem.title +'</a>'+
						'</span>'+
					'</span>'+
				'</li>'
			);
		});

		// 新闻列表渲染
		addNewList();

		// 视频列表渲染
		renderData.videoList.forEach(function(elem){
			$(".s-video-wrapper").append(
				'<div class="video-col">'+
					'<div class="item-wrapper">'+
						'<img class="img" width="195px" height="267px" src="'+ elem.img +'">'+
					'</div>'+
					'<div class="v-txt">'+
						'<div class="text">'+
							'<a href="'+elem.link+'" target="_blank" title="' + elem.title + '">' + elem.title + '</a>'+
						'</div>'+
						'<div class="score">'+
							'<span class="score-star">'+
								'<span class="star-active" style="width:' + elem.width + '"></span>'+
							'</span>'+elem.score+''+
						'</div>'+
					'</div>'+
				'</div>'
			);
		});

		// 更多产品渲染
		renderData.moreList.forEach(function(elem){
			$(".bdbriwrapper-a").append(
				'<a href="'+elem.link+'">'+
					'<span style="background-position:'+elem.img+';"></span>'+elem.title+''+
				'</a>'
			);
		});

		// 皮肤设置渲染
		for(var i = renderData.skinType.length - 1;i > -1;i--){
			$(".s-skin-nav").prepend(
				'<li class="skin-nav '+ (i == 0 ? 'choose-nav' : '') +'">'+
					'<span class="skinnav-nav-name">'+ renderData.skinType[i] +'</span>'+
				'</li>'
			);
		}

		var sSkinContentNum = 0;
		for(var i = 0;i < renderData.skinType.length;i++ ){
			$(".s-skin-content").append($("<li class='skin-img-item'>").append(
				function(){
					var itemImgHtml = ""; 
					for (var j = 1; j <= 4;j++) {
						if(i >= 2){
							sSkinContentNum = (i+1)*10 + j;
						}else{
							sSkinContentNum++;
						}
						itemImgHtml = itemImgHtml + '<img class="skin-img-item-img" src="./images/bg'+ sSkinContentNum +'.jpg">';
					};
					return itemImgHtml;
				}
				
			));
		}

	}

	function addNewList(){
		renderData.newList.forEach(function(elem){
			$(".s-news-list-wrapper").append($("<div class='s-news-item s-news-item-tpl-3 s-opacity-blank8 first'>").append(
					function(){
						var h2Html = '<h2>'+
										'<span class="status prefix-0"></span>'+
										'<a href="'+elem.link+'" title="' + elem.title + '" target="_blank" data-title="'+ elem.title +'"class="s-yahei"> '+ elem.title +'</a>'+
									'</h2>';
						var picHtml = '<div class="s-pic-content">'+
											'<ul class="s-news-content-imgs clearfix">'+
												'<li class="item-img-wrap"><img class="s-news-img" src="'+ elem.img[0] +'"></li><li class="item-img-wrap"><img class="s-news-img" src="'+ elem.img[1] +'"></li><li class="item-img-wrap mr0"><img class="s-news-img" src="'+ elem.img[2] +'"></li>'+
											'</ul>'+
										'</div>';
						var fromHtml = '<div class="from">'+
											'<span class="src-net">'+
												'<a href="" target="_blank" data-src="1" data-click="LOG_LINK">'+ elem.from +'</a>'
											'</span>'+
											'<span class="src-time">'+ elem.time +'</span>'+
										'</div>';

						return h2Html + picHtml + fromHtml;
					}
				)
			)
		});
	}

	render();


	// 更多产品
	$(".more").mouseenter(function(){
    	$(".bdbri").css("display","block");
    });
    $(".bdbri").mouseleave(function(){
    	$(".bdbri").css("display","none");
    });

    // 登陆框显现
    var loginFlag = true;
    $(".login").click(function(){
    	if(loginFlag){
    		$("#passport-login").show();
    	}
    });
    // 关闭登陆框
    $(".close-btn").click(function(){
    	$("#passport-login").hide();
    });

    // 获取输入焦点
	$(".pass-text-input").focus(function(){
	    $(this).css("border-color","#4791ff");
	    var labelIcon = $(this).prev().attr("id");
	    if(labelIcon == "userNameLabel"){
	  		$(this).prev().css("background-position","0 -108px");
		  }else{
		  	$(this).prev().css("background-position","0 -187px");
		  }
	});

	// 失去输入焦点
	$(".pass-text-input").blur(function(){
	  	$(this).css("border-color","#ddd");
	  	$("#userNameLabel").css("background-position","0 -68px");
	  	$("#passwordLabel").css("background-position","0 -147px");
	});

	// 显示清除
	$(".pass-text-input").keydown(function(){
		var clearBtn = $(this).next();
	    clearBtn.show();
	});
	// 清除输入内容
	$(".pass-clearbtn").click(function(){
		var clearInput = $(this).prev();
	    clearInput.val("");
	    clearInput.focus();
	    $(this).hide();
	});

	// 登陆
	$(".pass-button").click(function(){
		var input = $(".pass-text-input");
		if (input[0].value=="" || input[1].value==""){
			alert("请输入正确的账号和密码");
		}else{
			$("#passport-login").hide();
			$(".home").css("visibility","visible");
			$("header").css("border-bottom","1px solid #eee")
			$(".logo-white").css("margin-top","25px");
			$("#s_wrap").css("visibility","visible");
			$("footer").removeClass("footer");
			$("footer").css({"text-aline":"center","padding-top":"45px"});
			$(".login").text(input[0].value);
			loginFlag = false;
			isLogin = true;
		}
	});

	// 皮肤设置
	$(".skin").click(function(){
		$(".s-skin-layer").animate({top:"0px",opacity:"1"},800);
	});
	$(".s-skin-setting,#content,#s_wrap,footer").click(function(){
		$(".s-skin-layer").animate({top:"-233px",opacity:"0.8"},500);
	});
	$(".skin-nav").click(function(){
		$(this).addClass("choose-nav");
		$(this).siblings().removeClass("choose-nav");
		$(".skin-img-item").hide();
		$(".skin-img-item").eq($(this).index()).show();
	});
	$(".skin-img-item-img").click(function(){
		var itemImg = $(this).attr("src");
		$(".s-skin-container").css("background-image","url("+itemImg+")");
		$(".logo-white").attr("src","./images/logo_white.png");
		$("header").addClass("skin-header");
		$(".top-form-origin #btn").css({"background-color":"#ccc","-webkit-box-shadow":"0 1px 3px rgba(0,0,0,0.2)"});
		$(".top-form-origin #text").css({"border":"0","height":"22px"});
		$("footer a,footer span").css("color","#fff");
		$(".s-more-bar").css({"color":"#fff","text-shadow":"1px 1px 1px rgba(0, 0, 0, .2)"});
		$(".rect").css("background-position","-104px -24px");
		$(".weather-mod").css("color","#fff");
		$(".to-top").css({"background":"none","border":"0"});
		$(".icon-mask").css("color","#fff");
		$(".icon").css("background-position","-37px -10px");
	});

	// tap切换
	$(".s-menu-item").click(function(){
		if($(this).index() == 1){
			isLogin = true;
		}else{
			isLogin = false;
		}
		$(this).addClass("choose-menu");
		if($(this).index() == 0){
			$(".icon-item .mine-icon").css("background-position","-145px 0");
		}else{
			$(".icon-item .mine-icon").css("background-position","-15px 0");
		}
		$(this).siblings().removeClass("choose-menu");
		$(".s-content").hide();
		$(".s-content").eq($(this).index()).show();
	});

	

	
	var formTop = $(".top-form-origin").offset().top,formFixed;
	var newsTop = $(".s-menu-container").offset().top,newsFixed;
	var toTop = $(".top-feed").offset().top,toFixed;
	var isAddNewsList = false;
	var screenHeight = $(window).height();
	var newLeft = $(".s-news-rank-wrapper").offset().left;

	// 滚动事件监听
	$(window).scroll(function(){
		var scrollTop = $(window).scrollTop();
		var listHeiht=$("#s_ctner_contents").height();
		isAddNewsList=scrollTop+1*screenHeight >= listHeiht ? true : false;
		formFixed = scrollTop > formTop-90;
		newsFixed = scrollTop > newsTop-96;
		toFixed = scrollTop > toTop;

		// 输入框浮动
		if(formFixed){
			$(".s-top-wrap").show();
			$(".content").css("visibility","hidden");
			$(".top-img").fadeIn("slow");
		}else{
			$(".s-top-wrap").hide();
			$(".content").css("visibility","visible");
			$(".top-img").hide();
		}

		// 时事热点浮动
		if(newsFixed){
			$(".s-news-rank-wrapper").addClass("news-rank-wrapper");
			$(".s-news-rank-wrapper").css({top:"80px",left:newLeft});
		}else{
			$(".s-news-rank-wrapper").removeClass("news-rank-wrapper");
			$(".s-news-rank-wrapper").css({top:"auto",left:"auto"});
		}

		// 回到顶部出现
		if(toFixed){
			$(".top-feed").show();
		}else{
			$(".top-feed").hide();
		}	
		// 加载更多新闻
		if(isAddNewsList && isLogin){
			addNewList();
			$("#s_ctner_contents,.s-xmancard").height("auto");
		}
	});

	$("#container").mousewheel(function(event,delta){
		if(delta == -1 && $(window).height() >= $("body").height() && isLogin){
			addNewList();
			$("#s_ctner_contents,.s-xmancard").height("auto");
		}
	});

	// 回到顶部
	$(".top-feed").click(function(){
		$("body,html").animate({scrollTop:0},"speed");
	});
});